import http from '../../utils/httpRequest.js'

let roleListTest = [
    {id: 1, shopId: 1, name: "超级管理员", permissionList: ["*"], description: "所有权限"},
    {
        id: 2,
        name: "后台管理员",
        permissionList: [
            "user:list", "user:update",
            "goods:list", "goods:add", "goods:delete", "goods:update",
            "order:list", "order:add", "order:delete", "order:update",
        ],
        description: ""
    },
    {
        id: 3,
        shopId: 1,
        name: "游客",
        permissionList: ["user:list", "goods:list", "order:list", "sysUser:list", "role:list", "shop:list"],
        description: "查看系统"
    },
]

export function getRoleList(params) {
    http({
        ...params,
        path: "/system/role/list",
        testData: JSON.parse(JSON.stringify(roleListTest)), // 触发视图更新
    })
}

// 添加角色
export function addRole(params) {
    http({
        ...params,
        method: "post",
        path: "/system/role",
        testData: function () {
            params.data.id = Math.random()
            roleListTest.push(params.data)
            return true
        }
    })
}

// 删除角色
export function deleteRole(params) {
    http({
        ...params,
        method: "delete",
        path: "/system/role/" + params.roleId,
        testData: function () {
            for (let i = 0; i < roleListTest.length; i++)
                if (roleListTest[i].id === params.roleId)
                    roleListTest.splice(i, 1)
            return true
        }
    })
}

// 更新角色
export function updateRole(params) {
    http({
        ...params,
        method: "put",
        path: "/system/role/" + params.data.id,
        testData: function () {
            for (let i = 0; i < roleListTest.length; i++)
                if (roleListTest[i].id === params.data.id)
                    roleListTest[i] = params.data
            return true
        }
    })
}

export function getPermissionTree(params) {
    http({
        ...params,
        path: "/system/role/permissionTree",
        testData: [
            {
                label: '用户管理',
                permission: "userAdmin", // 树形控件的id，叶子节点对应一个唯一的权限
                children: [
                    {
                        label: '用户列表',
                        permission: 'user:list'
                    },
                ]
            },
            {
                label: '管理员管理',
                permission: "sysUserAdmin",
                children: [
                    {
                        label: '管理员列表',
                        permission: 'sysUser:list'
                    },
                ]
            }
        ]
    })
}
